package com.loubii.sunshineSchool.ui.avtivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.app.MainApplication;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.util.PermissionsUtil;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Response;


public class SSLoginActivity extends AppCompatActivity {

    private EditText userName, passWord;
    private ImageView unameClear, pwdClear;
    private TextView touristLogin, userReg;
    private CircleImageView iv_icon;
    private Button login;
    private SharedPreferences sp;
    private SharedPreferences.Editor ed;
    private static final int TIME_INTERVAL = 2000;
    private long mBackPressed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        PermissionsUtil.checkAndRequestPermissions(this);
        init();//初始化组件
        ViewClick();//注册组件点击事件
    }

    private void init() {
        userName = (EditText) findViewById(R.id.et_userName);
        passWord = (EditText) findViewById(R.id.et_password);
        unameClear = (ImageView) findViewById(R.id.iv_unameClear);
        pwdClear = (ImageView) findViewById(R.id.iv_pwdClear);
        iv_icon = (CircleImageView) findViewById(R.id.iv_icon);
        touristLogin = (TextView) findViewById(R.id.link_tourist);
        userReg = (TextView) findViewById(R.id.link_signup);
        login = (Button) findViewById(R.id.btn_login);
    }

    private void login() {
        final String username = userName.getText().toString();
        final String password = passWord.getText().toString();

        if (username.isEmpty()) {
            Toast.makeText(getApplicationContext(), "帐号不能为空", Toast.LENGTH_LONG).show();
            return;
        } else if (password.isEmpty()) {
            Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }

        HttpBin.login(SSLoginActivity.this, username, password, new HttpBin.ResponseListener<Integer>() {
            @SuppressLint("ShowToast")
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    Toast.makeText(SSLoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    ((MainApplication) getApplication()).setUid(response.body());
                    Intent intent = new Intent();
                    intent.setClass(SSLoginActivity.this, SSMainTabActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(SSLoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }

        });


    }

    private void goReg() {
        userReg.setTextColor(Color.rgb(0, 0, 0));
        Intent intent = new Intent(getApplicationContext(), SSRegActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    private void ViewClick() {

        userReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goReg();
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });

        touristLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SSLoginActivity.this, SSMainTabActivity.class);
                intent.putExtra("login_user", "tourist");
                startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            System.exit(0);
            return;
        } else {
            Toast.makeText(getBaseContext(), "再按一次返回退出程序", Toast.LENGTH_SHORT).show();
        }

        mBackPressed = System.currentTimeMillis();
    }

}
